package org.itboys.advertisement.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.advertisement.dto.LinkTagArray;
import org.itboys.advertisement.entity.Advertisement;
import org.itboys.advertisement.service.AdvertisementService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 广告
 * @author weisky
 *
 */
@RestController 
@RequestMapping("/advertisement") 
public class AdvertisementController extends BaseController{

	@Autowired
	private AdvertisementService advertisementService;
	
	/**
	 * 跳到广告位管理页面 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toListVM")
	public ModelAndView toListVM(Long adzoneId, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("adzoneId", adzoneId);
		return new ModelAndView("/advertisement/advertisement");
	} 
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response){
		Page<Advertisement> page = advertisementService.listAndPage(request);
		JsonPageUtils.renderJsonPage(page.getTotalPages(), page.getData(), response);
	}
	
	/**
	 * 新增或者修改
	 * @param adzone
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(Advertisement advertisement, 
			HttpServletRequest request, HttpServletResponse response){
		boolean result = advertisementService.insert(advertisement);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/**
	 * 获取一条数据
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getById")
	public void getById(Long id, HttpServletRequest request, HttpServletResponse response){
		AjaxUtils.renderJson(response, advertisementService.getById(id));
	}
	
	/**
	 * 删除一条数据
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delete")
	public void delete(Long id, HttpServletRequest request, HttpServletResponse response){
		boolean result = advertisementService.dele(id);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/**
	 * 打开导链标签页
	 * @param advertisementId
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/openLinkTagVM")
	public ModelAndView openLinkTagVM(Long advertisementId, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("advertisementId", advertisementId);
		model.addAttribute("linkTag", advertisementService.getById(advertisementId).getLinkTags());
		return new ModelAndView("/advertisement/linktag");
	}
	
	/**
	 * 对广告信息设置导链标签
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/setLinkTag")
	public void setLinkTag(LinkTagArray linkTagArray, Long advertisementId, 
			HttpServletRequest request, HttpServletResponse response){
		boolean result = advertisementService.setLinkTag(linkTagArray, advertisementId);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
}
